//闭包防止全局污染
(function(window){
    //构造大方块对象
    function BigSquare() {

    }

    //创建方块节点的方法
    BigSquare.prototype.createSquare = function () {
        var bigBox = $('.sqaure_box');
        var firstUl = $('.sqaure_box ul:first-of-type');
        var addUl = document.createElement('ul');
        bigBox.insertBefore(addUl, firstUl);
        addUl.innerHTML = ` <li></li>
                            <li></li>
                            <li></li>
                            <li></li>`;
    }

    //小黑块随机方法
    BigSquare.prototype.randomBlackSquare = function () {
        var randomshu = shu(0, 3);
        var firstli = $('.sqaure_box ul:first-of-type li');
        firstli[randomshu].className = 'black';
    }

    //移除方块节点方法
    BigSquare.prototype.removeSquare = function () {
        var lastUl = $('.sqaure_box ul:last-of-type');
        lastUl.remove();
    }
    // 让BigSquare对象变成全局对象
    window.myBigSquare = BigSquare;
})(window);

//构造分数框对象
//分数加一
(function(window){
    function ScoreBox() {
        var scoreBox = $('#score');
        this.score = scoreBox.value;
        this.Score = function () {
            this.score++;
            scoreBox.value = this.score;
        }
    }
    window.myScoreBox = ScoreBox;
})(window);



(function(window){
    //构造游戏对象
    function Game() {
        //gameover属性设置当定时器结束后不许点击加分
        this.gameover = true;
        this.speed = 0;
        this.i = 1;
        that = this;
    }
    //方块下移方法
    Game.prototype.move = function (sec) {
        var speed = 0;
        var count = 0;
        var first = -100;
        var bigBox = $('.sqaure_box');
        //实例化BigSquare对象,增加和少节点
        var little = new myBigSquare();
        var timeId = setInterval(function () {
            //定时器里面的this指向window!!!
            that.speed += that.i;
            bigBox.style.top = first + that.speed + 'px';
            //下移到据顶部为0,增加减少节点
            if (bigBox.offsetTop >= 0) {
                count++;
                little.createSquare();
                bigBox.style.top = -100 + 'px';
                little.randomBlackSquare();
                if (count > 1) {
                    little.removeSquare();
                }
                //最后一行判断是否有black样式,有则定时器结束
                var lastChildren = bigBox.lastElementChild.children;
                // console.log(lastChildren);
                for (var i = 0; i < lastChildren.length; i++) {
                    if (lastChildren[i].classList.contains('black')) {
                        this.gameover = false;
                        clearInterval(window.timeId);
                        alert('游戏结束');
                    }
                }
                first = -100;
                that.speed = 0;
            }

        }, sec);

        // 让timeId成为全局变量
        window.timeId = timeId;
    }

    //方块结束方法
    Game.prototype.over = function () {

        var sec = 20;
        // 使用Game对象的move方法
        that.move(sec);

        var bigBox = $('.sqaure_box');

        //大盒子点击事件
        bigBox.onclick = function (e) {
            console.log(that.gameover);
            //使用$()方法获取节点剩下最后一个的时候返回的是单个节点,使用下标找不到
            var blackSquare = document.querySelectorAll('.sqaure_box .black');
            var lastBlackSquare = blackSquare.length - 1;
            //当点击的是最后一个黑块时
            if (e.target === blackSquare[lastBlackSquare]) {
                if (that.gameover) {
                    //移除黑块
                    blackSquare[lastBlackSquare].classList.remove('black');

                    //每点成功一次加分
                    // 分数框对象实例化
                    var score_box = new myScoreBox();
                    score_box.Score();
                    //分数每五分加速一次
                    if (score_box.score % 5 == 0) {
                        that.i++;
                    }
                }

            }
            else {
                that.gameover = false;
                console.log(that.gameover);
                clearInterval(window.timeId);
                alert('游戏结束');
            }
        }
    }
    window.myGame = Game;
})(window);
